package com.nl.sq;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public class QueueStack<T> {

	Queue<T> queue1 = new ArrayBlockingQueue<T>(20);
	Queue<T> queue2 = new ArrayBlockingQueue<T>(20);
	int length;

	public QueueStack() {
		length = 0;
	}

	public T pop() {
		if(queue1.isEmpty()){
			if(queue2.isEmpty()){
				return null;
			}
			for(int i=0,len=queue2.size()-1;i<len;i++){
				queue1.offer(queue2.poll());
			}
			length--;
			return queue2.poll();
		}else{
			for(int i=0,len=queue1.size()-1;i<len;i++){
				queue2.offer(queue1.poll());
			}
			length--;
			return queue1.poll();
		}
	}

	public void push(T t) {
		if(queue1.isEmpty()&&queue2.isEmpty()||!queue1.isEmpty()){
			queue1.offer(t);
		}else{
			queue2.offer(t);
		}
		length++;
	}

	public boolean isEmpty() {
		return length == 0;
	}

	public static void main(String[] args) {
		QueueStack<Integer> stack = new QueueStack<Integer>();
		for (int i = 0; i < 10; i++) {
			stack.push(i);
		}

		while (!stack.isEmpty()) {
			System.out.println(stack.pop());
		}

	}
}
